//
//  ClassifyListViewController.m
//  MeterMart
//
//  Created by ENERGY on 2018/11/3.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "ClassifyListViewController.h"
#import "OtherListTableViewCell.h"
#import "HotelViewController.h"
#import "SSKeychain.h"
#import "RecommendTableViewCell.h"
#import "MallProductDetailViewController.h"
@interface ClassifyListViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)NSMutableArray * cellArr;
@property (nonatomic, assign)int currentPage;
- (NSString *)getDeviceUUID;
@end

@implementation ClassifyListViewController(networking)

//推荐列表
- (void)getRecommendData{
    NSDictionary * dic = @{
                           @"typeid":self.typeID,
                           @"page":@(self.currentPage),
                           @"size":@"6",
                           @"only":[self getDeviceUUID],
                           @"firstpage":@"0"
                           };
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"listiosApi/index" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([responseObject[@"sign"] isEqualToString:@"1"]) {
            if (self.currentPage == 1) {
                [self.cellArr removeAllObjects];
            }
            [self.cellArr addObjectsFromArray:responseObject[@"list"][@"unfinequality"]];
            [self.tableView reloadData];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end

@implementation ClassifyListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = getColor(mainColor);
    [self.view addSubview:self.tableView];
    self.currentPage = 1;
    [self getRecommendData];//网络接口
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
#pragma mark - init
- (NSMutableArray *)cellArr{
    if (!_cellArr) {
        _cellArr = @[].mutableCopy;
    }return _cellArr;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT - NAVI_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = 172/WIDTH_6_SCALE;
        [_tableView registerClass:[OtherListTableViewCell class] forCellReuseIdentifier:NSStringFromClass([OtherListTableViewCell class])];
        [_tableView registerClass:[RecommendTableViewCell class] forCellReuseIdentifier:NSStringFromClass([RecommendTableViewCell class])];
        _tableView.backgroundColor = getColor(mainColor);
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.currentPage = 1;
            [self getRecommendData];
        }];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            self.currentPage ++;
            [self getRecommendData];
        }];
    }return _tableView;
}
#pragma mark - tableViewDataSource和tableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = nil;
    if ([self.typeID isEqualToString:@"1"]) {
        RecommendTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([RecommendTableViewCell class]) forIndexPath:indexPath];
        cell.model = self.cellArr[indexPath.row];
        return cell;
    }else{
        OtherListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([OtherListTableViewCell class]) forIndexPath:indexPath];
        cell.model = self.cellArr[indexPath.row];
        return cell;
    }return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.typeID isEqualToString:@"1"]) {
        return 150/WIDTH_6_SCALE;;
    }else{
        return 172/WIDTH_6_SCALE;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cellArr.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.typeID isEqualToString:@"1"]) {
        MallProductDetailViewController * vc = [[MallProductDetailViewController alloc] init];
        vc.produceID = [self.cellArr[indexPath.row] objectForKey:@"id"];
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        HotelViewController * vc = [[HotelViewController alloc] init];
        vc.prodeceID = [self.cellArr[indexPath.row] objectForKey:@"typeid_goods"];
        vc.navigationItem.title = [self.cellArr[indexPath.row] objectForKey:@"name"];
        vc.typeID = [self.cellArr[indexPath.row] objectForKey:@"type_id"];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark - 获取唯一标识符
- (NSString *)getDeviceUUID{
    NSString * currentDeviceUUIDStr = [SSKeychain passwordForService:@"com.liqiu"account:@"uuid"];
    if (currentDeviceUUIDStr == nil || [currentDeviceUUIDStr isEqualToString:@""]){
        NSUUID * currentDeviceUUID  = [UIDevice currentDevice].identifierForVendor;
        currentDeviceUUIDStr = currentDeviceUUID.UUIDString;
        currentDeviceUUIDStr = [currentDeviceUUIDStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
        currentDeviceUUIDStr = [currentDeviceUUIDStr lowercaseString];
        [SSKeychain setPassword: currentDeviceUUIDStr forService:@"com.liqiu"account:@"uuid"];
    }return currentDeviceUUIDStr;
}
@end
